package io.github.biezhi.elves.thread;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.SocketTimeoutException;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

import javax.annotation.Resource;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;

import io.github.biezhi.elves.common.Constants;
import io.github.biezhi.elves.ok.http.FastHttpClient;
import io.github.biezhi.elves.property.CityProperty;
import io.github.biezhi.elves.qcc.model.CompBasic;
import io.github.biezhi.elves.qcc.model.CompQccCrawl;
import io.github.biezhi.elves.service.QccService;
import io.github.biezhi.elves.utils.StringUtils;

@Component
public class QccRunnable implements Runnable {
	
	private String qccCookie;
	private List<CompBasic> compBasicList;
	
	
	public QccRunnable() {
		super();
	}
	public QccRunnable(String qccCookie,List<CompBasic> compBasicList) {
		this.qccCookie = qccCookie;
		this.compBasicList = compBasicList;
	}

	@Autowired
	private QccService qccServiceBean;
	private static QccService qccService;
	
	@Bean
	public QccService getQccServiceBean(){
		if(qccServiceBean!=null){
			qccService = qccServiceBean;
			return qccService;
		}
		return null;
	}
	
	private String url = "https://www.qcc.com/";
	
	
	@Override
	public void run() {
			for (CompBasic compBasic : compBasicList) {
				CompQccCrawl compQccCrawl = new CompQccCrawl();
				//获取企业id
				Long compId = compBasic.getCompId();
//				String pripid = compBasic.getPripid();
				//获取企业名称
				String compName = compBasic.getCompName();
				//获取企业详细url
				String htmlContentByCompName = getHtmlContentByCompName(compName);
				String compDetailUrl = getCompDetailUrl(htmlContentByCompName, compName);
				
				//获取企查查信息id
				String qccId = "";
				//获取企业详情html
				String compDetailHtml = "";
				//企业图谱json数据
				String companyBusinessmap = "";
				//投资族谱json数据
				String companyRelation = "";
				//关联图谱json数据
				String companyMuhou = "";
				//股权结构图json数据
				String companyGuquan = "";
				if (compDetailUrl != "") {
					qccId = compDetailUrl.substring(compDetailUrl.indexOf("_")+1, compDetailUrl.indexOf("."));
					compDetailHtml = getHtmlContentByCompDetailUrl(compDetailUrl);
//					companyBusinessmap = getHtmlContentByParamAndKeyNo("cms_businessmap", qccId);
//					companyRelation = getHtmlContentByParamAndKeyNo("cms_map", qccId);
//					companyMuhou = getHtmlContentByParamAndKeyNo("company_muhouAction", qccId);
//					companyGuquan = getHtmlContentByParamAndKeyNo("cms_guquanmap2", qccId);
				}
				
				compQccCrawl.setCompId(compId);
//				compQccCrawl.setPripid(pripid);
				compQccCrawl.setCompName(compName);
				compQccCrawl.setQccId(qccId);
				compQccCrawl.setCompDetailHtml(compDetailHtml);
//				compQccCrawl.setCompanyBusinessmap(companyBusinessmap);
//				compQccCrawl.setCompanyRelation(companyRelation);
//				compQccCrawl.setCompanyMuhou(companyMuhou);
//				compQccCrawl.setCompanyGuquan(companyGuquan);
				
//				System.out.println(compDetailHtml);
				
				//将compQccCrawl插入到数据库
				qccService.insertSpiderData(compQccCrawl);
				//将表字段更新为已爬取
				qccService.updateSpiderState(compId);
			}
	}
	
	String proxyIp = Constants.PROXY_IP;
	int proxyPort = Constants.PROXY_PORT;
	//通过get请求获取网页内容
	private String getHtmlContentByCompName(String compName) {
		String result = "";
		sleepTime(10000L);
		try {
			io.github.biezhi.elves.ok.http.Response okHttpResponse;
//			Proxy proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress(Constants.PROXY_IP, Constants.PROXY_PORT));
			okHttpResponse = FastHttpClient
					.newBuilder()
//					.proxy(proxy)
					.build()
					.get()
					.addHeader("cookie", qccCookie)
					.url(url+"search?key="+compName)
					.build()
					.execute();
			System.out.println(url+"search?key="+compName+"qccCookie:"+qccCookie);
			result = okHttpResponse.string();
//			System.out.println(result);
			if (result.contains("<script>window.location.href='https://www.qcc.com/")
					|| result.contains("user_login_normal")) {
				System.err.println("操作过于频繁，休眠30s！" + Thread.currentThread().getName()+"========>"+qccCookie);
				sleepTime(30000L);
//				resetProxyIp();
				result = getHtmlContentByCompName(compName);
			}
			if(!StringUtils.isNullOrEmpty(result)){
				return result;
			}
		} catch (SocketTimeoutException e) {
			System.err.println("===连接超时，重新调用getHtmlContentByCompName(compName)方法===");
			sleepTime(10000L);
//			resetProxyIp();
			getHtmlContentByCompName(compName);
		} catch (Exception e) {
			e.printStackTrace();
			System.err.println("===其他异常，重新调用getHtmlContentByCompName(compName)方法===");
			sleepTime(10000L);
//			resetProxyIp();
			getHtmlContentByCompName(compName);
		}
		return result;
	}
	
	//通过get请求和公司详情页面的url获取网页内容
	private String getHtmlContentByCompDetailUrl(String compDetailUrl) {
		String result = "";
		try {
			sleepTime(10000L);
			io.github.biezhi.elves.ok.http.Response okHttpResponse;
//			Proxy proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress(Constants.PROXY_IP, Constants.PROXY_PORT));
			okHttpResponse = FastHttpClient
					.newBuilder()
//					.proxy(proxy)
					.build()
					.get()
					.addHeader("cookie", qccCookie)
					.url(url+compDetailUrl+"?tab=run")
					.build()
					.execute();
			
			result = okHttpResponse.string();
			if (result.contains("<script>window.location.href='https://www.qcc.com/")
					|| result.contains("user_login_normal")) {
				System.err.println("操作过于频繁，休眠30s！" + Thread.currentThread().getName()+"========>"+qccCookie);
				sleepTime(30000L);
//				resetProxyIp();
				result = getHtmlContentByCompDetailUrl(compDetailUrl);
			}
			if(!StringUtils.isNullOrEmpty(result)){
				return result;
			}
		} catch (SocketTimeoutException e) {
			System.err.println("===连接超时，重新调用getHtmlContentByCompDetailUrl(compDetailUrl)方法===");
			sleepTime(10000L);
//			resetProxyIp();
			getHtmlContentByCompDetailUrl(compDetailUrl);
		} catch (Exception e) {
			e.printStackTrace();
			System.err.println("===其他异常，重新调用getHtmlContentByCompDetailUrl(compDetailUrl)方法===");
			sleepTime(10000L);
//			resetProxyIp();
			getHtmlContentByCompDetailUrl(compDetailUrl);
		}
		return result;
	}
	
	//通过get请求和参数(企业图谱,投资族谱,关联图谱,股权结构图对应的参数)和keyNo获取json数据
	private String getHtmlContentByParamAndKeyNo(String param,String keyNo) {
		String result = "";
		String visitUrl = url+param+"?keyNo="+keyNo;
		sleepTime(10000L);
		try {
			io.github.biezhi.elves.ok.http.Response okHttpResponse;
//			Proxy proxy = new Proxy(Proxy.Type.HTTP,new InetSocketAddress(Constants.PROXY_IP, Constants.PROXY_PORT));
			okHttpResponse = FastHttpClient
					.newBuilder()
//					.proxy(proxy)
					.build()
					.get()
					.addHeader("cookie", qccCookie)
					.url(visitUrl)
					.build()
					.execute();
			
			result = okHttpResponse.string();
//			System.out.println(result);
			if (result.contains("<script>window.location.href='https://www.qcc.com/")
					|| result.contains("user_login_normal")) {
				System.err.println("操作过于频繁，休眠30s！" + Thread.currentThread().getName()+"========>"+qccCookie);
				sleepTime(30000L);
//				resetProxyIp();
				result = getHtmlContentByParamAndKeyNo(param,keyNo);
			}
			if(!StringUtils.isNullOrEmpty(result)){
				return result;
			}
		} catch (SocketTimeoutException e) {
			System.err.println("===连接超时，重新调用getHtmlContentByParamAndKeyNo(param,keyNo)方法===");
			sleepTime(10000L);
//			resetProxyIp();
			getHtmlContentByParamAndKeyNo(param,keyNo);
		} catch (Exception e) {
			e.printStackTrace();
			System.err.println("===其他异常，重新调用getHtmlContentByParamAndKeyNo(param,keyNo)方法===");
			sleepTime(10000L);
//			resetProxyIp();
			getHtmlContentByParamAndKeyNo(param,keyNo);
		}
		return result;
	}
	//休眠
	private void sleepTime(Long sleepTime) {
		try {
			Thread.sleep(sleepTime);
			System.err.println("休眠" + sleepTime + "毫秒后，往下执行");
		} catch (Exception e){}
	}
	
	private void resetProxyIp() {
		try{
			Map<String,Object> proxyIpMap = StringUtils.getHttpProxyIp(1);
			String proxyJson = JSONObject.toJSONString(proxyIpMap);
			System.out.println(proxyJson);
			proxyIp = StringUtils.getValueForKey("proxyHost", proxyIpMap);
			proxyPort = StringUtils.getIntValueForKey("proxyPort", proxyIpMap);
			
			System.out.println("代理ip{{}}==>" + proxyIp + "，代理端口{{}}==>" + proxyPort);
		} catch (Exception e) {
			System.err.println("获取第三方代理ip接口异常{{}}==>" + StringUtils.getErrorInfoFromException(e));
		}
	}

	/**
	 * 获取公司详情页面的url
	 * @param result 通过getHtmlContentByCompName爬取出来的的页面
	 * @return 公司详情页面的url
	 */
	public String getCompDetailUrl(String result,String compName){
		Document doc = Jsoup.parse(result);
		Elements allAElements = doc.select("a.ma_h1");
		ListIterator<Element> htmlAResultIter = allAElements.listIterator();
		while (htmlAResultIter.hasNext()) {
			Element element = (Element) htmlAResultIter.next();
			String text = element.text();
			//如果该内容等于compName,表明需要的就是这家公司，获取该a标签下的href
			if (text.equals(compName) || text.equals(compName.replace("（", "(").replace("）", ")"))) {
				String compDetailUrl = element.attr("href");
				return compDetailUrl;
			}
		}
		return "";
	}

}
